﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Intelsi.BusinessLogic;
using Intelsi.Entity;
using Microsoft.Practices.Unity;

namespace Intelsi.WB
{
    public partial class FrmBanco : System.Web.UI.Page
    {

        [Dependency]
        public IBancoBL bancoBL { get; set; }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                var acc = Request.QueryString["acc"];

                if (acc != string.Empty)
                {

                    if (acc == "nuevo")
                    {

                        AccionHF.Value = acc;
                        HfBanco.Value = "0";

                        
                        var banco = new Banco();
                        BindBanco(banco);


                    }

                    if (acc == "editar")
                    {

                        AccionHF.Value = acc;
                        var IdBanco = Int32.Parse(Request.QueryString["IdBanco"]);
                        var banco = bancoBL.GetAllFromBancoById(IdBanco);
                        BindBancoActualizar(banco);


                    }
                }

            }

           
        }

        protected void Button1_Click(object sender, EventArgs e)
        {


            Banco banco = new Banco();
            BindBanco(banco);


            if (AccionHF.Value == "editar")
            {
                bancoBL.ActualizarBanco(banco);
             
            }
            else if (AccionHF.Value == "nuevo")
            {
                bancoBL.AddBanco(banco);
                
            }

            Response.Redirect("frmListarBancos.aspx");
        }

        private void BindBanco(Banco banco)
        {

            banco.IdBanco = Int32.Parse(HfBanco.Value);
            banco.Nombre = txtBanco.Text;
            banco.Tipo = bool.Parse(DowTipo.SelectedValue);
        }

        private void BindBancoActualizar(Banco banco)
        {

            HfBanco.Value = banco.IdBanco.ToString();
            txtBanco.Text = banco.Nombre;
            DowTipo.SelectedValue = banco.Tipo.ToString();


        }

        protected void Button2_Click(object sender, EventArgs e)
        {
            Response.Redirect("frmListarBancos.aspx");
        }

    }
}